package org.flexspeedup.blog.admin.views
{
import flash.events.Event;
import flash.events.MouseEvent;

import org.flexspeedup.blog.admin.events.CollectionManagerEvent;
import org.flexspeedup.model.IListModel;

import spark.components.Button;
import spark.components.List;

//----------------------------------
//  Events
//----------------------------------
[Event(name="editSelectedItem", type="org.flexspeedup.blog.admin.events.CollectionManagerEvent")]
[Event(name="deleteSelectedItem", type="org.flexspeedup.blog.admin.events.CollectionManagerEvent")]
[Event(name="createNewItem", type="org.flexspeedup.blog.admin.events.CollectionManagerEvent")]
[Event(name="navigateToNextPage", type="org.flexspeedup.blog.admin.events.CollectionManagerEvent")]
[Event(name="navigateToPreviousPage", type="org.flexspeedup.blog.admin.events.CollectionManagerEvent")]

//----------------------------------
//  Skin states
//----------------------------------
[SkinState("normal")]
[SkinState("disabled")]

/**
 * A skinnable component that allows to manage a collection of items: navigate, 
 * create, edit and delete items.
 */
public class CollectionManagerList extends List
{

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new <code>CollectionManagerList</code> instance.
     */
    public function CollectionManagerList()
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    //  Skin parts
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    //  editButton
    //----------------------------------
    [SkinPart(required="false")]
    public var editButton:Button;

    //----------------------------------
    //  deleteButton
    //----------------------------------
    [SkinPart(required="false")]
    public var deleteButton:Button;

    //----------------------------------
    //  newButton
    //----------------------------------
    [SkinPart(required="false")]
    public var newButton:Button;

    //----------------------------------
    //  previousPageButton
    //----------------------------------
    [SkinPart(required="false")]
    public var previousPageButton:Button;

    //----------------------------------
    //  nextPageButton
    //----------------------------------
    [SkinPart(required="false")]
    public var nextPageButton:Button;
    
    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  model
    //----------------------------------
    /**
     *  @private
     */    
    private var _model:IListModel;
    /**
     *  @private
     */
    private var modelChanged:Boolean;
    
    [Bindable] 
    /**
     * The data model for this collection
     */
    public function get model():IListModel
    {
        return _model;
    }
    
    public function set model(value:IListModel):void
    {
        _model = value;
        modelChanged = true;
    }

    //--------------------------------------------------------------------------
    //
    //  Overridden Methods - SkinnableComponent
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    override protected function partAdded(partName:String, instance:Object):void
    {
        super.partAdded(partName, instance);
        
        switch (instance)
        {
            case (editButton):
            {
                editButton.addEventListener(
                    MouseEvent.CLICK, editButton_clickHandler);
                break;
            }
            case (deleteButton):
            {
                deleteButton.addEventListener(
                    MouseEvent.CLICK, deleteButton_clickHandler);
                break;
            }
            case (newButton):
            {
                newButton.addEventListener(
                    MouseEvent.CLICK, newButton_clickHandler);
                break;
            }
            case (previousPageButton):
            {
                previousPageButton.addEventListener(
                    MouseEvent.CLICK, previousPageButton_clickHandler);
                break;
            }
            case (nextPageButton):
            {
                nextPageButton.addEventListener(
                    MouseEvent.CLICK, nextPageButton_clickHandler);
                break;
            }
        }
    }

    /**
     *  @private
     */
    override protected function partRemoved(partName:String, instance:Object):void
    {
        super.partRemoved(partName, instance);
        
        switch (instance)
        {
            case (editButton):
            {
                editButton.removeEventListener(
                    MouseEvent.CLICK, editButton_clickHandler);
                break;
            }
            case (deleteButton):
            {
                deleteButton.removeEventListener(
                    MouseEvent.CLICK, deleteButton_clickHandler);
                break;
            }
            case (newButton):
            {
                newButton.removeEventListener(
                    MouseEvent.CLICK, newButton_clickHandler);
                break;
            }
            case (previousPageButton):
            {
                previousPageButton.removeEventListener(
                    MouseEvent.CLICK, previousPageButton_clickHandler);
                break;
            }
            case (nextPageButton):
            {
                nextPageButton.removeEventListener(
                    MouseEvent.CLICK, nextPageButton_clickHandler);
                break;
            }
        }
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  dispatchCollectionManagerEvent
    //----------------------------------
    /**
     * @private
     */
    private function dispatchCollectionManagerEvent(event:CollectionManagerEvent):void
    {
        event.currentPage = model.resultSet.currentPage;
        event.selectedItem = selectedItem;
        dispatchEvent(event);
    }
    
    //--------------------------------------------------------------------------
    //
    //  Event Handlers
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    //  editButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function editButton_clickHandler(event:MouseEvent):void
    {   
        event.stopImmediatePropagation();
        
        dispatchCollectionManagerEvent(
            new CollectionManagerEvent(
                CollectionManagerEvent.EDIT_SELECTED_ITEM
            )
        );
    }
    
    //----------------------------------
    //  deleteButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function deleteButton_clickHandler(event:MouseEvent):void
    {
        event.stopImmediatePropagation();
        
        dispatchCollectionManagerEvent(
            new CollectionManagerEvent(
                CollectionManagerEvent.DELETE_SELECTED_ITEM
            )
        );
    }

    //----------------------------------
    //  newButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function newButton_clickHandler(event:MouseEvent):void
    {
        event.stopImmediatePropagation();
        
        dispatchCollectionManagerEvent(
            new CollectionManagerEvent(
                CollectionManagerEvent.CREATE_NEW_ITEM
            )
        );
    }
    
    //----------------------------------
    //  previousPageButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function previousPageButton_clickHandler(event:MouseEvent):void
    {
        event.stopImmediatePropagation();
        
        dispatchCollectionManagerEvent(
            new CollectionManagerEvent(
                CollectionManagerEvent.NAVIGATE_TO_PREVIOUS_PAGE
            )
        );
    }
    
    //----------------------------------
    //  nextPageButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function nextPageButton_clickHandler(event:MouseEvent):void
    {
        event.stopImmediatePropagation();
        
        dispatchCollectionManagerEvent(
            new CollectionManagerEvent(
                CollectionManagerEvent.NAVIGATE_TO_NEXT_PAGE
            )
        );
    }

}
}